package com.test;

import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.InetAddress;

public class LocalOnlyServer {
    public static void main(String[] args) {
        int port = 8080; // 要绑定的端口

        try {

            // 创建一个只允许本机连接的 ServerSocket
            ServerSocket serverSocket = new ServerSocket(port, 1, InetAddress.getLoopbackAddress());
            NioServerSocketChannel serverSocketChannel = new NioServerSocketChannel();
            System.out.println( "ip："+InetAddress.getLocalHost().getHostAddress());
            System.out.println( "loop ip："+InetAddress.getLoopbackAddress().getHostAddress());

            System.out.println( "localhost："+InetAddress.getLocalHost().getCanonicalHostName());
            System.out.println("本地服务器已启动，监听端口：" + port);

            while (true) {
                // 等待客户端连接
                Socket socket = serverSocket.accept();

                System.out.println("接收到来自本机的连接：" + socket.getInetAddress());

                // 处理客户端请求...

                // 关闭与客户端的连接
                socket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
